'use strict';

const { getPagination } = require('../tools');

const Service = require('egg').Service;

class HomeService extends Service {
  async findAll({ page_num, page_size }) {
    const { offset, limit } = getPagination(page_num, page_size);

    try {
      const res = await this.ctx.model.Home.findAndCountAll({
        offset,
        limit,
      });
      return {
        page_num: +page_num,
        page_size: +page_size,
        ...res,
      };
    } catch (error) {
      return {
        isErr: true,
        error,
      };
    }
  }

  async findById(id) {
    return this.ctx.model.Home.findByPk(id);
  }

  async create(user) {
    return this.ctx.model.Home.create(user);
  }

  async update(id, user) {
    return this.ctx.model.Home.update(user, {
      where: { id },
    });
  }

  async delete(id) {
    return this.ctx.model.User.destroy({
      where: { id },
    });
  }
}

module.exports = HomeService;
